package com.ddxz.hospital.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/9/23
 */

public class ListHandler extends BaseTypeHandler<List<Object>> {
    private static final String TYPE_CHAR = "text";
    private static final String TYPE_INT = "int4";
    private static final String TYPE_FLOAT = "float4";
    private static final String TYPE_LONG = "int8";
    private static final String TYPE_DOUBLE = "float8";

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<Object> ts, JdbcType jdbcType) throws SQLException {
        if (ts.size() < 1) {
            preparedStatement.setArray(i, (Array)null);
        } else {
            Connection conn = preparedStatement.getConnection();
            String var7 = ts.get(0).getClass().getName();
            byte var8 = -1;
            switch(var7.hashCode()) {
                case -2056817302:
                    if (var7.equals("java.lang.Integer")) {
                        var8 = 0;
                    }
                    break;
                case -527879800:
                    if (var7.equals("java.lang.Float")) {
                        var8 = 2;
                    }
                    break;
                case 398795216:
                    if (var7.equals("java.lang.Long")) {
                        var8 = 1;
                    }
                    break;
                case 761287205:
                    if (var7.equals("java.lang.Double")) {
                        var8 = 3;
                    }
                    break;
                case 1195259493:
                    if (var7.equals("java.lang.String")) {
                        var8 = 4;
                    }
            }

            Array array;
            switch(var8) {
                case 0:
                    array = conn.createArrayOf("int4", ts.toArray());
                    break;
                case 1:
                    array = conn.createArrayOf("int8", ts.toArray());
                    break;
                case 2:
                    array = conn.createArrayOf("float4", ts.toArray());
                    break;
                case 3:
                    array = conn.createArrayOf("float8", ts.toArray());
                    break;
                case 4:
                    array = conn.createArrayOf("text", ts.toArray());
                    break;
                default:
                    List<String> tmp = new ArrayList(ts.size());
                    ts.forEach((item) -> {
                        tmp.add(JSONObject.toJSONString(item));
                    });
                    array = conn.createArrayOf("text", tmp.toArray());
            }

            preparedStatement.setArray(i, array);
        }
    }

    @Override
    public List<Object> getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return this.array2List(resultSet.getArray(s));
    }

    private List<Object> array2List(Array array) {
        if (array == null) {
            return null;
        } else {
            try {
                Object[] tmp = (Object[])((Object[])array.getArray());
                List<Object> rst = new ArrayList(tmp.length);
                String name = tmp.getClass().getName();
                byte var6 = -1;
                switch(name.hashCode()) {
                    case -2071011078:
                        if (name.equals("[Ljava.lang.Long;")) {
                            var6 = 1;
                        }
                        break;
                    case -1935445726:
                        if (name.equals("[Ljava.lang.Integer;")) {
                            var6 = 0;
                        }
                        break;
                    case -175516795:
                        if (name.equals("[Ljava.lang.Double;")) {
                            var6 = 3;
                        }
                        break;
                    case 48646404:
                        if (name.equals("[Ljava.lang.Float;")) {
                            var6 = 2;
                        }
                        break;
                    case 392722245:
                        if (name.equals("[Ljava.lang.String;")) {
                            var6 = 4;
                        }
                }

                Object[] var7;
                int var8;
                int var9;
                Object item;
                switch(var6) {
                    case 0:
                        var7 = tmp;
                        var8 = tmp.length;

                        for(var9 = 0; var9 < var8; ++var9) {
                            item = var7[var9];
                            rst.add((Integer)item);
                        }

                        return rst;
                    case 1:
                        var7 = tmp;
                        var8 = tmp.length;

                        for(var9 = 0; var9 < var8; ++var9) {
                            item = var7[var9];
                            rst.add((Long)item);
                        }

                        return rst;
                    case 2:
                        var7 = tmp;
                        var8 = tmp.length;

                        for(var9 = 0; var9 < var8; ++var9) {
                            item = var7[var9];
                            rst.add((Float)item);
                        }

                        return rst;
                    case 3:
                        var7 = tmp;
                        var8 = tmp.length;

                        for(var9 = 0; var9 < var8; ++var9) {
                            item = var7[var9];
                            rst.add((Double)item);
                        }

                        return rst;
                    case 4:
                        var7 = tmp;
                        var8 = tmp.length;

                        for(var9 = 0; var9 < var8; ++var9) {
                            item = var7[var9];
                            rst.add((String)item);
                        }

                        return rst;
                    default:
                        var7 = tmp;
                        var8 = tmp.length;

                        for(var9 = 0; var9 < var8; ++var9) {
                            item = var7[var9];
                            rst.add(JSONObject.parseObject(item.toString()));
                        }

                        return rst;
                }
            } catch (SQLException var11) {
                var11.printStackTrace();
                return null;
            }
        }
    }

    @Override
    public List<Object> getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return this.array2List(resultSet.getArray(i));
    }

    @Override
    public List<Object> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return this.array2List(callableStatement.getArray(i));
    }
}
